package com.example.manager;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

import android.util.Log;

import com.example.base.BaseScene;
import com.example.scene.GameScene;
import com.example.scene.HighScores;
import com.example.scene.LoadingScene;
import com.example.scene.LoginScene;
import com.example.scene.MainMenuScene;
import com.example.scene.ResultScene;
import com.example.scene.ResultSceneForTimeMode;
import com.example.scene.SplashScene;
import com.example.scene.TimerMode;

public class SceneManager {
	private BaseScene splashScene;
	private BaseScene menuScene;
	private BaseScene gameScene;
	private BaseScene loadingScene;
	private BaseScene highScene;
	private BaseScene loginScene;
	private BaseScene resultScene;
	private BaseScene helpScene;
	private BaseScene timerMode;
	
	private static final SceneManager INSTANCE = new SceneManager();
	private SceneType currentSceneType = SceneType.SCENE_SPLASH;
	private BaseScene currentScene;
	private Engine engine = ResourcesManager.getInstance().engine;

	public enum SceneType {
		SCENE_SPLASH, 
		SCENE_MENU, 
		SCENE_GAME, 
		SCENE_LOADING,
		SCENE_LOGIN,
		SCENE_HIGHSCORE,
		SCENE_RESULT,
		SCENE_HELP,
		SCENE_TIMERMODE
	}

	// ---------------------------------------------
	// CLASS LOGIC
	// ---------------------------------------------

	public void setScene(BaseScene scene) {
		engine.setScene(scene);
		currentScene = scene;
		currentSceneType = scene.getSceneType();
	}

	public void setScene(SceneType sceneType) {
		switch (sceneType) {
		case SCENE_MENU:
			setScene(menuScene);
			break;
		case SCENE_GAME:
			setScene(gameScene);
			break;
		case SCENE_SPLASH:
			setScene(splashScene);
			break;
		case SCENE_LOADING:
			setScene(loadingScene);
			break;
		case SCENE_LOGIN:
			setScene(loginScene);
			break;
		case SCENE_HIGHSCORE:
			setScene(highScene);
			break;
		case SCENE_HELP:
			setScene(helpScene);
			break;
		case SCENE_RESULT:
			setScene(resultScene);
			break;
		case SCENE_TIMERMODE:
			setScene(timerMode);
			break;
		default:
			break;
		}
	}

	// ---------------------------------------------
	// GETTERS AND SETTERS
	// ---------------------------------------------

	public static SceneManager getInstance() {
		return INSTANCE;
	}

	public SceneType getCurrentSceneType() {
		return currentSceneType;
	}

	public BaseScene getCurrentScene() {
		return currentScene;
	}
	public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
	{
	    ResourcesManager.getInstance().loadSplashScreen();
	    splashScene = new SplashScene();
	    currentScene = splashScene;
	    pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}
	private void disposeSplashScene()
	{
	    ResourcesManager.getInstance().unloadSplashScreen();
	    splashScene.disposeScene();
	    splashScene = null;
	}
	public void createMenuScene()
	{
	    ResourcesManager.getInstance().loadMenuResources();
	    menuScene = new MainMenuScene();
	    loadingScene = new LoadingScene();
	    SceneManager.getInstance().setScene(menuScene);
	    disposeSplashScene();
	}
	
	public void loadGameScene(final Engine mEngine)
	{
	    setScene(loadingScene);
	    ResourcesManager.getInstance().unloadMenuTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadGameResources();
	            Log.d("tag","loadGameScene");
	            gameScene = new GameScene();
	            setScene(gameScene);
	        }
	    }));
	}
	public void loadTimerMode(final Engine mEngine)
	{
		setScene(loadingScene);
	    ResourcesManager.getInstance().unloadMenuTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadGameResources();
	            Log.d("tag","loadGameScene");
	           // gameScene = new GameScene();
	            setScene(new TimerMode());
	        }
	    }));
	}
/*	public void loadLoginScene(final Engine mEngine)
	{
	    ResourcesManager.getInstance().unloadMenuTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadLoginGraphics();
	            Log.d("tag","loadGameScene");
	            loginScene = new LoginScene();
	            setScene(loginScene);
	        }
	    }));
	}*/
	public void loadHighScoresScene(final Engine mEngine)
	{
	    ResourcesManager.getInstance().unloadMenuTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadHighScores();
	            highScene = new HighScores();
	            setScene(highScene);
	        }
	    }));
	}
	
	public void loadResultScene(final Engine mEngine)
	{
		// ResultScene rs = new ResultScene();
        // setScene(rs);
	    //setScene(resultScene);
	    //ResourcesManager.getInstance().unloadGameTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResultScene rs = new ResultScene();
	            setScene(rs);
	        }
	    }));
	}

	public void loadMenuScene(final Engine mEngine)
	{
	    if(SceneManager.getInstance().getCurrentScene().getSceneType()==SceneType.SCENE_GAME){
	    	Log.d("pos", "dispose");
	    	gameScene.disposeScene();
	    }
	    setScene(loadingScene);
	    //else
	    	//highScene.disposeScene();
	    ResourcesManager.getInstance().unloadGameTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadMenuTextures();
	            menuScene = new MainMenuScene();
	            setScene(menuScene);
	        }
	    }));
	}
	public void loadResultSceneFromTimeMode(final Engine mEngine) {
		   mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
		    {
		        public void onTimePassed(final TimerHandler pTimerHandler) 
		        {
		            mEngine.unregisterUpdateHandler(pTimerHandler);
		            ResultSceneForTimeMode rs = new ResultSceneForTimeMode();
		            setScene(rs);
		        }
		    }));
	}
	
	public void loadMenuSceneFromTimeMode(final Engine mEngine)
	{
	    if(SceneManager.getInstance().getCurrentScene().getSceneType()==SceneType.SCENE_TIMERMODE){
	    	Log.d("pos", "dispose");
	    	timerMode.disposeScene();
	    }
	    setScene(loadingScene);
	    //else
	    	//highScene.disposeScene();
	    ResourcesManager.getInstance().unloadTimeModeTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadMenuTextures();
	            menuScene = new MainMenuScene();
	            setScene(menuScene);
	        }
	    }));
	}

}
